"""
    模拟浏览器实现
        1、导入模块
        2、创建套接字
        3、建立连接
        4、拼接请求报文
        5、发送请求报文
        6、接受服务器返回的数据
        7、处理数据
        8、保存
        9、关闭连接
"""

import socket

tcp_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

tcp_client.connect(("www.baidu.com", 80))

request_line = "GET / HTTP/1.1\r\n"
request_header = "Host:www.baidu.com\r\n"
request_blank = "\r\n"

request_data = request_line + request_header + request_blank

tcp_client.send(request_data.encode())

recv_data = tcp_client.recv(40960)

# print(recv_data.decode())
recv_text = recv_data.decode("utf-8")

loc = recv_text.find("\r\n\r\n")
web_data = recv_text[loc+4:]

print(web_data)
with open("a.html", "w") as file:
    file.write(web_data)

tcp_client.close()

